{% extends "base.html" %}

{% load promgen %}
{% load i18n %}

{% block title %}
Promgen / Rule / {{ rule.name }}
{% endblock %}

{% block content %}
<div class="page-header">
    <h1>Rule: {{ rule.name }}</h1>
</div>

{% breadcrumb rule  %}

<div class="panel panel-danger" v-cloak v-show="alertLabelsRule.has('{{rule.name}}')" data-service="{{rule.name}}">
    <div class="panel-heading">
        <a @click.prevent="toggleTarget('alerts-service-{{rule.name|slugify}}')" class="btn btn-danger btn-sm" role="button">Alerts</a>
    </div>
    <table id="alerts-service-{{rule.name|slugify}}" class="table table-bordered table-condensed collapse">
        <tr v-for="(alert, index) in globalAlerts" v-if="alert.labels.alertname == '{{rule.name}}'">
            {% include 'promgen/alert_row.html' %}
        </tr>
    </table>
</div>

<div class="panel panel-primary">
    <div class="panel-heading">{{rule.name}}</div>
    <div class="panel-body">
        <pre v-pre>{{rule|rule_dict|pretty_yaml}}</pre>
    </div>
    <div class="panel-footer">
        <a href="{% url 'rule-edit' rule.pk %}" class="btn btn-warning btn-sm">{% trans "Edit Rule" %}</a>
    </div>
</div>

{% if rule.parent %}
<div class="panel panel-default">
    <div class="panel-heading">Parent</div>
    <table class="table">
        <tr>
            <td>
                <a href="{{rule.parent.get_absolute_url}}">{{rule.parent.name}}</a>
            </td>
            <td>
                <code v-pre data-href="{% url 'rule-test' rule.parent.pk %}">
                    {{ rule.parent|rulemacro }}
                </code>
            </td>
        </tr>
    </table>
</div>
{% endif %}

{% if rule.overrides.count %}
<div class="panel panel-default">
    <div class="panel-heading">Child Rules</div>
    <table class="table">
        <tr>
            <th>Rule</th>
            <th>Clause</th>
            <th>Owner</th>
        </tr>
        {% for r in rule.overrides.all %}
        <tr>
            <td class="col-xs-2">
                <a href="{% url 'rule-detail' r.pk %}">{{ r.name }}</a>
                <ul>
                    {% for k,v in r.labels.items|dictsort:0 %}
                    <li class="label label-primary">{{k}}:{{v}}</li>
                    {% endfor %}
                </ul>
            </td>
            <td class="col-xs-8">
                <code v-pre data-href="{% url 'rule-test' r.id %}">{{ r|rulemacro }}</code>
            </td>
            <td class="col-xs-2"><a href="{{ r.content_object.get_absolute_url }}">{{ r.content_object.name }}</a></td>
        </tr>
        {% endfor %}
    </table>
</div>
{% endif %}

{% endblock %}
